<?php
/**
 * 直接检查数据库中的系统配置
 */

// 使用SQLite数据库
$dbPath = __DIR__ . '/database/sqlite.db';

try {
    $dsn = "sqlite:{$dbPath}";
    $pdo = new PDO($dsn);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "<h2>系统配置检查</h2>\n";
    
    // 检查存储相关配置
    $stmt = $pdo->prepare("SELECT name, value FROM system_config WHERE type = 'storage' ORDER BY name");
    $stmt->execute();
    $configs = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<table border='1' style='border-collapse: collapse;'>\n";
    echo "<tr><th>配置名</th><th>配置值</th></tr>\n";
    
    $allowExts = '';
    foreach ($configs as $config) {
        echo "<tr><td>{$config['name']}</td><td>{$config['value']}</td></tr>\n";
        if ($config['name'] === 'allow_exts') {
            $allowExts = $config['value'];
        }
    }
    echo "</table>\n";
    
    // 检查图片格式支持
    echo "<h2>图片格式支持检查</h2>\n";
    if (empty($allowExts)) {
        echo "<p style='color: red;'>⚠️ 未找到 allow_exts 配置，需要初始化</p>\n";
        
        // 插入默认配置
        $defaultExts = 'doc,gif,ico,jpg,jpeg,mp3,mp4,p12,pem,png,rar,xls,xlsx';
        $stmt = $pdo->prepare("INSERT INTO system_config (type, name, value) VALUES ('storage', 'allow_exts', ?) ON DUPLICATE KEY UPDATE value = ?");
        $stmt->execute([$defaultExts, $defaultExts]);
        echo "<p style='color: green;'>✓ 已插入默认配置: {$defaultExts}</p>\n";
        
    } else {
        $allowedExts = array_map('trim', explode(',', strtolower($allowExts)));
        $imageExts = ['jpg', 'jpeg', 'png', 'gif'];
        $missingExts = array_diff($imageExts, $allowedExts);
        
        if (empty($missingExts)) {
            echo "<p style='color: green;'>✓ 图片格式支持完整: " . implode(', ', array_intersect($imageExts, $allowedExts)) . "</p>\n";
        } else {
            echo "<p style='color: orange;'>⚠️ 缺少图片格式: " . implode(', ', $missingExts) . "</p>\n";
            
            // 添加缺少的格式
            $newExts = array_unique(array_merge($allowedExts, $imageExts));
            $newExtsStr = implode(',', $newExts);
            $stmt = $pdo->prepare("UPDATE system_config SET value = ? WHERE type = 'storage' AND name = 'allow_exts'");
            $stmt->execute([$newExtsStr]);
            echo "<p style='color: green;'>✓ 已更新配置: {$newExtsStr}</p>\n";
        }
    }
    
    // 检查其他必要配置
    echo "<h2>其他配置检查</h2>\n";
    $requiredConfigs = [
        ['storage', 'type', 'local'],
        ['storage', 'name_type', 'xmd5'],
        ['storage', 'link_type', 'none'],
    ];
    
    foreach ($requiredConfigs as [$type, $name, $defaultValue]) {
        $stmt = $pdo->prepare("SELECT value FROM system_config WHERE type = ? AND name = ?");
        $stmt->execute([$type, $name]);
        $value = $stmt->fetchColumn();
        
        if ($value === false) {
            $stmt = $pdo->prepare("INSERT INTO system_config (type, name, value) VALUES (?, ?, ?)");
            $stmt->execute([$type, $name, $defaultValue]);
            echo "<p style='color: green;'>✓ 已添加配置 {$type}.{$name} = {$defaultValue}</p>\n";
        } else {
            echo "<p>✓ 配置 {$type}.{$name} = {$value}</p>\n";
        }
    }
    
} catch (PDOException $e) {
    echo "<p style='color: red;'>数据库连接失败: " . $e->getMessage() . "</p>\n";
    echo "<p>数据库路径: {$dbPath}</p>\n";
    echo "<p>文件是否存在: " . (file_exists($dbPath) ? '是' : '否') . "</p>\n";
}
?>
